﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// TreeHelper 的摘要说明，主要是跟前台配合使用实现自定义Tree导航等功能
/// </summary>
public class TreeHelper
{
    
    public TreeHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static List<Node> convertData2Nodes(ArrayList table)
    {
        List<Node> nodes = new List<Node>();
        Node node = null;
        int id = -1;
        int pid = -1;
        string name = null;
        foreach(Hashtable t in table)
        {
            /*这里要注意，hashtable中的前3项必须为id,pid,name*/
            id = (int)t[id];
            pid = (int)t[pid];
            name = t[name].ToString();
            node = new Node(id, pid, name);
            nodes.Add(node);
        }

        /*这里设置Node之间的节点关系，遍历设置相互父子关系*/
        for (int i = 0; i<nodes.Count; i++)
        {
            Node n = nodes[i];
            for(int j = i + 1; j < nodes.Count; j++)
            {
                Node m = nodes[j];
                if(m.Pid == n.Id)
                {
                    n.Children.Add(m);
                    m.Parent = n;
                }else if (n.Pid == m.Id)
                {
                    m.Children.Add(n);
                    n.Parent = m;
                }
            }
        }

        /*设置展开图标，这个看情况，主要是为了跟前台页面接轨*/
        foreach(Node n in nodes)
        {
            setNodeIcon(n,null,null);
        }
        return nodes;
    }

    /*设置排序功能，主要给前台排序用*/
    public static List<Node> getSortedNodes(ArrayList table,int defaultExpandLevel, int currentLevel)
    {
        List<Node> result = new List<Node>();
        /*这里可以直接调用convertData2Nodes，也就是开发者可以直接调用排序方法，不需要再调用非排序方法，再排序*/
        List<Node> nodes = convertData2Nodes(table);
        List<Node> rootNodes = getRootNodes(nodes);

        foreach(Node node in nodes)
        {
            addNode(result, node, defaultExpandLevel, currentLevel);
        }
        return result;
    }

    private static void addNode(List<Node> result, Node node, int defaultExpandLevel, int currentLevel)
    {
        result.Add(node);

        if (defaultExpandLevel >= currentLevel)
        {
            node.IsExpand = true;
        }
        if (node.isLeaf())
        {
            return;
        }
        /*递归调用*/
        for(int i = 0;i<node.Children.Count; i++)
        {
            addNode(result, node.Children[i], defaultExpandLevel, currentLevel + 1);
        }
    }
    /*过滤是否显示的node*/
    public static List<Node> filterVisiableNodes(List<Node> nodes)
    {
        List<Node> result = new List<Node>();

        foreach(Node node in nodes)
        {
            if(node.isRoot() || node.isParentExpand())
            {
                setNodeIcon(node,null,null);/*未改，这里2个null需要改变的*/
                result.Add(node);
            }
        }
        return result;
    }

    /*先获取所有的跟节点*/
    private static List<Node> getRootNodes(List<Node> nodes)
    {
        List<Node> root = new List<Node>();
        foreach (Node node in nodes)
        {
            if (node.isRoot())
            {
                root.Add(node);
            }
        }
        return root;
    }

    //设置展开图标，如有的话穿入左，下图片地址即可
    private static void setNodeIcon(Node n, string imgUrl_R, string imgUrl_B)
    {
        if (imgUrl_R == null && imgUrl_B == null)
        {
            n.Icon = null;
            return ;
        }else if(n.Children.Count>0 && n.IsExpand)
        {
            n.Icon = imgUrl_R;
        }
        else
        {
            if (n.Children.Count > 0 && !n.IsExpand)
            {
                n.Icon = imgUrl_B;
            }
        }
    }
}